package com.vision.sales.customer.cl.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import com.sencha.gxt.data.shared.TreeStore.TreeNode;
import com.vision.sales.cm.db.data.AdvancePayment;
import com.vision.sales.cm.db.data.AppliedAdvancePayment;

/**
 * 
 * @author Mark
 *
 * @param <T>
 */
public class AdvancePaymentsTreeNode implements TreeNode<AdvancePaymentsTreeNode> {
	
	Object object;
	
	List<AdvancePaymentsTreeNode> children;
	
	AdvancePaymentsTreeNode() {
		
	}
	
	public AdvancePaymentsTreeNode(Object object) {
		this.object = object;
	}

	@Override
	public List<AdvancePaymentsTreeNode> getChildren() {
		if (children != null)
			return children;
		
		if (object instanceof AdvancePayment) {
			Set<AppliedAdvancePayment> set = ((AdvancePayment) object).getAppliedAdvancePayments();
			children = new ArrayList<AdvancePaymentsTreeNode>();
			for (AppliedAdvancePayment a : set) {
				children.add( new AdvancePaymentsTreeNode(a) );
			}
		}
		
		return children;
	}

	@Override
	public AdvancePaymentsTreeNode getData() {
		return this;
	}
	
	public Object getObject() {
		return object;
	}
	
}